package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import artid.com.arttogoa.R;

import artid.com.arttogoa.adapter.AddressManagement_Adapter;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.AddressManagementParser;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout.OnRefreshListener;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullableListView;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称：AddressManagement_Activity   
 * 类描述：地址管理页面   
 * 创建人：耿卫斌  
 * 创建时间：2016年5月16日 下午1:20:30      
 *
 */
public class AddressManagement_Activity extends BaseActivity implements OnClickListener{
	private Context mContext;
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private boolean isRefreshed = false;// 是否刷新
	private int page=1;
	private AddressManagement_Adapter adapter;
	private TextView addAddressBtn;
	private LinearLayout noDataLL;
	private RelativeLayout noDataLL_addAdressBtn;
	private List<AddressManagementParser> list;
	private List<AddressManagementParser> more_list;
	private Boolean isAddressManagementInetnt;//是否为“地址管理”跳转过来的(默认false)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address);
		setContent(this, getIntent());
		mContext = AddressManagement_Activity.this;
		initView();
	}

	private void initView(){
		mTittle.setText("地址管理");
		setBack();
		// 注册当前广播
		IntentFilter filter = new IntentFilter("com.AddressManagement_Activity.receiver");
		registerReceiver(broadcastReceiver, filter);
		isAddressManagementInetnt=getIntent().getBooleanExtra("isAddressManagementInetnt", false);
		list=new ArrayList<AddressManagementParser>();
		more_list=new ArrayList<AddressManagementParser>();
		noDataLL=(LinearLayout) findViewById(R.id.addressmanagement_noDataLL);
		noDataLL.setVisibility(View.GONE);
		noDataLL_addAdressBtn=(RelativeLayout) findViewById(R.id.addressmanagement_noDataLL_addAdress);
		noDataLL_addAdressBtn.setOnClickListener(this);
		addAddressBtn=(TextView) findViewById(R.id.addressmanagement_addAddress);
		addAddressBtn.setOnClickListener(this);
		mListView =(PullableListView) findViewById(R.id.addressmanagement_listview);
		mPullLayout = (PullToRefreshLayout)findViewById(R.id.addressmanagement_refresh_view);
		mPullLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				//下拉刷新
				page=1;
				isRefreshed=true;
				getData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
				//上拉加载
				page++;
				getData();
			}
		}); 
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				if(!isAddressManagementInetnt){
					//只要不是从个人页面的地址管理进入都可以点击选中地址列表将数据带过去
					Intent intent=new Intent();  
					intent.putExtra("address_id",more_list.get(position).getAddress_id());
					intent.putExtra("address_info", more_list.get(position).getAddressinfo());
					setResult(RESULT_OK, intent);  
					finish();
				}
			}
		});
		getData();
	}

	@Override
	public void onClick(View v) {
		Intent in;
		switch (v.getId()) {
		case R.id.addressmanagement_noDataLL_addAdress:
			//空数据的时候添加地址
			in=new Intent(AddressManagement_Activity.this,AddAddressActivity.class);
			in.putExtra("isAddressManagementInetnt", isAddressManagementInetnt);
			in.putExtra("addressIsNull", true);
			startActivityForResult(in, 1);
			break;
		case R.id.addressmanagement_addAddress:
			//有数据的添加地址
			in=new Intent(AddressManagement_Activity.this,AddAddressActivity.class);
			in.putExtra("isAddressManagementInetnt", isAddressManagementInetnt);
			in.putExtra("addressIsNull", false);
			startActivityForResult(in, 1);
			break;

		default:
			break;
		}

	}

	/**
	 * 获取地址列表
	 */
	private void getData(){
		VolleyBasic.getInstance(this).post(Cache.addressList,
				VolleyMap.getInstance().addressList(page,10),
				AddressManagementParser.class, new VolleyCallback() {
			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
				list=(List<AddressManagementParser>) vd.getList();
				if (isRefreshed == true) {
					more_list.clear();
					isRefreshed = false;
				}
				if (!list.isEmpty()) {
					more_list.addAll(list);
				}
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
					addAddressBtn.setVisibility(View.VISIBLE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					addAddressBtn.setVisibility(View.GONE);
					return;
				}
				if (adapter == null) {
					adapter=new AddressManagement_Adapter(mContext, more_list,isAddressManagementInetnt);
					mListView.setAdapter(adapter);
				}
				adapter.notifyDataSetChanged();
				Log.d("log", "解析后的数据："+more_list.toString());
			}
			public void onError(VolleyError volleyError) {
				mPullLayout.loadmoreFinish(mPullLayout.FAIL);
				if(more_list!=null && more_list.size()!=0){
					//这里是防止登陆的情况下增加关注数量
					noDataLL.setVisibility(View.GONE);
					addAddressBtn.setVisibility(View.VISIBLE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					addAddressBtn.setVisibility(View.GONE);
					return;
				}
			}
		});
	}

	/**
	 * 之前页面关闭数据接收
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
		// TODO Auto-generated method stub  
		super.onActivityResult(requestCode, resultCode, data);  
		if (requestCode == 1 && resultCode == RESULT_OK) { 
			//判断是否添加新地址
			if(data!=null){
				Boolean isRefresh=data.getBooleanExtra("isRefresh", false);
				if(isRefresh==true){
					page=1;
					isRefreshed=true;
					adapter=null;
					getData();
				}
			}
		}
	}

	// 广播监听
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getBooleanExtra("noAddress", false) == true) {
				//删除地址没数据的时候
				noDataLL.setVisibility(View.VISIBLE);
				addAddressBtn.setVisibility(View.GONE);
			}
			if (intent.getBooleanExtra("isCloseActivity", false) == true) {
				//是否是“我要发货”第一次添加地址成功就关闭当前地址列表页
				finish();
			}
			if (intent.getBooleanExtra("refreshData", false) == true) {
				//刷新数据
				page=1;
				isRefreshed=true;
				getData();
			}
		}
	};
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// 关闭广播
		unregisterReceiver(broadcastReceiver);
	}
}
